package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubContributionLogDao;
import com.game.server.db.dao.XyClubPremiumOplogDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.in.AbstractXYfindPlayersContributionReq;
import com.game.server.message.proto.out.XYclubContributionLogStruct;
import com.game.server.message.proto.out.XYclubPremiumOplogStruct;
import com.game.server.message.proto.out.XYfindPlayersContributionRsp;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class XYfindPlayersContributionReq extends AbstractXYfindPlayersContributionReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            int limit = 20;
            XYfindPlayersContributionRsp rsp = new XYfindPlayersContributionRsp();
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
            if (clubInfo != null) {
                XyClubMemberInfo tomberInfo = clubInfo.getPlayer().get(this.uid);
                XyClubMemberInfo memberInfo = clubInfo.getPlayer().get(client.getUserId());
                if (memberInfo != null && tomberInfo != null  && tomberInfo.getDel_type() == 0 && (memberInfo.getIsManager()==1 || memberInfo.getIsManager()==2 || memberInfo.getIsAccessory()==1)) {
                    JSONObject json = XyClubContributionLogDao.findSubordinatePlayerContributionPage(limit, this.page, this.clubid, this.uid,this.pid);
                    rsp.setAllpage(json.getInt("allpage"));
                    rsp.setAllcount(json.getInt("allcount"));
                    rsp.setAllContribution(json.getString("allContribution"));
                    rsp.setPage(this.page);
                    JSONArray array = json.getJSONArray("array");
                    XYclubContributionLogStruct[] contributionLogs = new XYclubContributionLogStruct[array.size()];
                    int index = 0;
                    for (Object ent : array) {
                        JSONObject object = JSONObject.fromObject(ent);
                        XYclubContributionLogStruct contributionLog = new XYclubContributionLogStruct();
                        contributionLog.setOptime(object.getString("createdate"));
                        contributionLog.setVal(object.getInt("val"));
                        contributionLog.setIntegral(object.getInt("integral"));
                        contributionLog.setRelevanceId(object.getInt("relevanceId"));
                        contributionLog.setProportion(object.getInt("proportion"));
                        contributionLogs[index] = contributionLog;
                        index++;
                    }

                    UserInfoData infoData = DataManager.getInstance().getMapUserData().get(this.uid);
                    rsp.setName(infoData.getName());
                    rsp.setHeadurl(infoData.getHead());
                    rsp.setUid(infoData.getId());
                    rsp.setContributionLogs(contributionLogs);
                }
            }
            client.Send(rsp);
        }
    }

}
